package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.RoleTypeConstants;
import com.campus.common.core.Result;
import com.campus.mapper.ManagerTeacherMapper;
import com.campus.mapper.UserMapper;
import com.campus.mapper.UserRoleMapper;
import com.campus.mapping.ManagerTeacherMapping;
import com.campus.model.TManagerTeacher;
import com.campus.model.TSysUser;
import com.campus.model.TSysUserRole;
import com.campus.model.dto.TManagerTeacherCreateDto;
import com.campus.model.dto.TManagerTeacherUpdateDto;
import com.campus.model.dto.TManagerTeacherUserCreateDto;
import com.campus.model.vo.TeacherSelectVo;
import com.campus.service.IManagerTeacherService;
import com.campus.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ManagerTeacherServiceImpl extends ServiceImpl<ManagerTeacherMapper, TManagerTeacher>
    implements IManagerTeacherService {

    @Autowired
    private ManagerTeacherMapper managerTeacherMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public Integer createTeacher(TManagerTeacherCreateDto teacherCreateDto) {
        TManagerTeacher tManagerTeacher = ManagerTeacherMapping.INSTANCE.CreatetoTeacher(teacherCreateDto);

        TSysUserRole userRole = new TSysUserRole();
        userRole.setUserId(tManagerTeacher.getLoginId());
        userRole.setRoleId(RoleTypeConstants.TEACHER);
        userRoleMapper.insert(userRole);
        return managerTeacherMapper.insert(tManagerTeacher);
    }

    @Override
    public Integer updateTeacher(TManagerTeacherUpdateDto teacherUpdateDto) {

        if (teacherUpdateDto.getLoginId() != null){
            QueryWrapper qw = new QueryWrapper();
            qw.eq("id",teacherUpdateDto.getId());
            //数据库中的teacher
            TManagerTeacher tManagerTeacher = managerTeacherMapper.selectOne(qw);

            if (tManagerTeacher.getLoginId() != null){
                if (tManagerTeacher.getLoginId() != teacherUpdateDto.getLoginId()){

                    //删除原来的userrole
                    QueryWrapper qw1 = new QueryWrapper();
                    qw1.eq("user_id",tManagerTeacher.getLoginId());
                    userRoleMapper.delete(qw1);

                    //添加新的userrole
                    TSysUserRole userRole = new TSysUserRole();
                    userRole.setUserId(teacherUpdateDto.getLoginId());
                    userRole.setRoleId(RoleTypeConstants.TEACHER);
                    userRoleMapper.insert(userRole);
                }
            }

        }
        TManagerTeacher tManagerTeacher = ManagerTeacherMapping.INSTANCE.UpdatetoTeacher(teacherUpdateDto);
        return managerTeacherMapper.updateById(tManagerTeacher);
    }

    @Override
    public TeacherSelectVo selectById(Long id) {
        TeacherSelectVo selectVo = ManagerTeacherMapping.INSTANCE.toSelectVo(managerTeacherMapper.selectById(id));
        return selectVo;
    }

    @Override
    public TeacherSelectVo selectByTeacherName(String teacherName) {

        QueryWrapper qw = new QueryWrapper();
        qw.like("teacher_name",teacherName);
        TeacherSelectVo selectVo = ManagerTeacherMapping.INSTANCE.toSelectVo(managerTeacherMapper.selectOne(qw));
        return selectVo;
    }

    @Override
    @Transactional
    public Integer createManagerTeacher(TManagerTeacherUserCreateDto createDto) {

        //先添加用户
        TSysUser user = ManagerTeacherMapping.INSTANCE.toUser(createDto);

        TManagerTeacher tManagerTeacher = ManagerTeacherMapping.INSTANCE.toTeacher(createDto);
        managerTeacherMapper.insert(tManagerTeacher);

        TManagerTeacher teacher = ManagerTeacherMapping.INSTANCE.toTeacher(createDto);
        teacher.setLoginId(user.getId());

        TSysUserRole userRole = new TSysUserRole();
        userRole.setUserId(user.getId());
        userRole.setRoleId(RoleTypeConstants.TEACHER);
        userMapper.insert(user);

        return userRoleMapper.insert(userRole);

    }
}
